package com.example.floatingpopulation.controller;

import com.example.floatingpopulation.common.CaptchaConfig;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import com.wf.captcha.utils.CaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping
public class CaptchaController {

    @RequestMapping("/captcha")
    public void captcha(@RequestParam String key, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 指定验证码的长宽和字符的个数
        SpecCaptcha captcha = new SpecCaptcha(135,34,4);
        captcha.setCharType(Captcha.TYPE_NUM_AND_UPPER);
        // 在后台保存一份验证码
        CaptchaConfig.CAPTCHA_MAP.put(key, captcha.text().toLowerCase());
        log.info("验证码：" + captcha.text());
        CaptchaUtil.out(captcha, request, response);

//        // 算术版验证码
//        ArithmeticCaptcha captcha = new ArithmeticCaptcha(135, 34);
//        captcha.setLen(2); // 设置几位数运算
//        captcha.getArithmeticString(); // 获取运算的公式
//        CaptchaConfig.CAPTCHA_MAP.put(key, captcha.text().toLowerCase()); // 在后台保存一份验证码
//        log.info("验证码：" + captcha.text());
//        CaptchaUtil.out(captcha, request, response);
    }
}
